package com.zyd.blog.personal.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zyd.blog.business.entity.UserInfo;
import com.zyd.blog.business.vo.UserInfoConditionVO;
import com.zyd.blog.persistence.beans.TbUserInfo;
import com.zyd.blog.persistence.mapper.TbUserInfoMapper;
import com.zyd.blog.personal.enuminterface.IsDelete;
import com.zyd.blog.personal.enuminterface.Status;
import com.zyd.blog.persistence.mapper.UserInfoMapper;
import com.zyd.blog.personal.service.UserInfoService;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.*;

/**
 * 用户详细信息业务逻辑层
 * @author mfy
 */
@Service
public class UserInfoServiceImpl implements UserInfoService {

    @Resource
    private UserInfoMapper userInfoMapper;

    @Resource
    private TbUserInfoMapper tbUserInfoMapper;



    @Override
    public Boolean isHaveUserInfo(Long userId){
        if(userId == null){
            return false;
        }
        List<Map<String,Object>> userInfos = getUserInfo(userId);
        return !(userInfos == null || userInfos.size() == 0);
    }

    @Override
    public List<Map<String,Object>> getUserInfo(Long userId){
        //  1. 查询用户详细信息
        Map<String,Object> condition = new HashMap<>(16);
        // 用户自身是否存在
        condition.put("isDelete", IsDelete.IS_DELETE_NO);
        // 用户自身状态是否正常
//        condition.put("status", Status.STATUS_YES);
        // 查询条件
        condition.put("userId",userId);
        return userInfoMapper.queryUserInfoByCondition(condition);
    }

    @Override
    public Map<String,Object> getUnitUserInfo(Long userId){
        List<Map<String,Object>> userInfo = getUserInfo(userId);
        if(userInfo == null || userInfo.size() == 0){
            return new HashMap<>(16);
        }
        return userInfo.get(0);
    }

    @Override
    public Map<String,Object> getUserSimpleInfo(Long userId){
        List<Map<String,Object>> userInfo = getUserInfo(userId);
        if(userInfo == null || userInfo.size() == 0){
            return new HashMap<>(0);
        }
        Map<String,Object> userSimple = new HashMap<>(16);
        userSimple.put("userId",userInfo.get(0).get("userId"));
        userSimple.put("nickname",userInfo.get(0).get("nickname"));
        userSimple.put("sex",userInfo.get(0).get("sex"));
        userSimple.put("headPicture",userInfo.get(0).get("headPicture"));
        return userSimple;
    }

    @Override
    public Boolean addUserInfo(Map<String,Object> userInfoCondition){
        userInfoCondition.put("updateTime",(new Date()).getTime());
        userInfoCondition.put("addTime",(new Date()).getTime());
        return userInfoMapper.addUserInfo(userInfoCondition) > 0;
    }

    @Override
    public Boolean updateUserInfo(Map<String,Object> map){
        map.put("updateTime",(new Date()).getTime());
        return userInfoMapper.updateUserInfo(map) > 0;
    }


    @Override
    public PageInfo<UserInfo> findPageBreakByCondition(UserInfoConditionVO vo) {

        PageHelper.startPage(vo.getPageNumber(), vo.getPageSize());

        List<TbUserInfo> tBUserInfos = tbUserInfoMapper.findPageBreakByCondition(vo);

        if (CollectionUtils.isEmpty(tBUserInfos)) {
            return null;
        }
        List<UserInfo> users = new ArrayList<>();
        for (TbUserInfo su : tBUserInfos) {
            users.add(new UserInfo(su));
        }
        PageInfo bean = new PageInfo<TbUserInfo>(tBUserInfos);
        bean.setList(users);
        return bean;
    }
}
